package com.tmz.cloudmall.cloudmallproduct.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tmz.cloudmall.cloudmallcommon.common.exception.ServiceException;
import io.netty.util.internal.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmz.cloudmall.cloudmallproduct.mapper.BrandMapper;
import com.tmz.cloudmall.cloudmallproduct.domain.Brand;
import com.tmz.cloudmall.cloudmallproduct.service.IBrandService;
import org.springframework.util.ObjectUtils;

/**
 * 品牌Service业务层处理
 * 
 * @author tmz
 * @date 2025-08-16
 */
@Service
public class BrandServiceImpl implements IBrandService 
{
    private static final Logger log = LoggerFactory.getLogger(BrandServiceImpl.class);
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询品牌
     * 
     * @param brandId 品牌主键
     * @return 品牌
     */
    @Override
    public Brand selectBrandByBrandId(Long brandId)
    {
        return brandMapper.selectBrandByBrandId(brandId);
    }

    /**
     * 查询品牌列表
     * 
     * @param brand 品牌
     * @return 品牌
     */
    @Override
    public List<Brand> selectBrandList(Brand brand)
    {
        return brandMapper.selectBrandList(brand);
    }

    /**
     * 新增品牌
     * 
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int insertBrand(Brand brand)
    {
        return brandMapper.insertBrand(brand);
    }

    /**
     * 修改品牌
     * 
     * @param brand 品牌
     * @return 结果
     */
    @Override
    public int updateBrand(Brand brand)
    {
        return brandMapper.updateBrand(brand);
    }

    /**
     * 批量删除品牌
     * 
     * @param brandIds 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deleteBrandByBrandIds(Long[] brandIds)
    {
        return brandMapper.deleteBrandByBrandIds(brandIds);
    }

    /**
     * 删除品牌信息
     * 
     * @param brandId 品牌主键
     * @return 结果
     */
    @Override
    public int deleteBrandByBrandId(Long brandId)
    {
        return brandMapper.deleteBrandByBrandId(brandId);
    }

    /**
     * 更新品牌显示状态
     * @param brand 需要更新的品牌
     * */
    @Override
    public int swtichStatus(Brand brand) {
        // 1. 参数校验
        if(ObjectUtils.isEmpty(brand)){
            log.error("品牌更新异常：未指定需要切换显示状态的品牌");
            throw new ServiceException("品牌更新异常：未指定需要切换显示状态的品牌");
        }
        // 2. 构造更新条件
        LambdaUpdateWrapper<Brand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Brand::getBrandId,brand.getBrandId())
                .set(Brand::getShowStatus,brand.getShowStatus());
        // 3. 执行更新并返回结果
         return brandMapper.update(updateWrapper);
    }
}
